using Cysharp.Threading.Tasks;
using M13.Game.Item;
using M13.Game.Item.Weapon;
using M13.Utility.Debugger;
using UnityEngine;


namespace M13.Game.Entity.Player
{
    public class PlayerActionAttack : PlayerStateBase
    {
        Vector3 _attackDir;
        Quaternion _attackRot;
        Transform _hitTransform;
        
        WeaponResourceConfig _resourceConfig;

        public PlayerActionAttack(string stateName)
        {
            this.StateName = stateName;
        }
        
        public override void Enter()
        {
            _resourceConfig = playerInventory.GetWeaponResourceConfig();
            StateInit();
            Attack();
        }

        public override void Update()
        {
            player.transform.rotation = Quaternion.Slerp(player.transform.rotation, _attackRot, 25 * Time.deltaTime);

            if (!playerInventory.CurrentHeldEmpty() && GetAnimPlayProgress() > 0.99f) return;
            stateMachine.SwitchOnState();
            
            DisplayBox.Display(player.transform.position + Vector3.up + _attackDir,_resourceConfig.attackBoxSize / 2, _attackRot, Color.white);
        }

        public override void FixedUpdate()
        {
            

        }

        public override bool TryEnter()
        {
            if(GetAnimPlayProgress() > 0.9f) return true;
            
            //进入条件：手持物品不为空；有按键指令；手持物品的类型是weapon
            if (!playerInventory.CurrentHeldEmpty() 
                && inputCtrlSystem.Execute0Input != 0
                && playerInventory.GetItemProperties().ItemType == ItemType.Weapon) return true;
            return false;
        }

        public override bool TryExit()
        {
            return GetAnimPlayProgress() >= 0.9f;
        }

        public override string GetAnimName()
        {
            return _resourceConfig.weaponAttackAnimation;
        }

        private async void Attack()
        {
            var animPlayProgressOnAttackPoint = UniTask.WaitUntil(() => GetAnimPlayProgress() > _resourceConfig.normalizedAttackTimeStamp && IsCurrentAnimPlaying(_resourceConfig.weaponAttackAnimation));
            await UniTask.WhenAll(animPlayProgressOnAttackPoint);
            
            Collider[] hitInfos;
            hitInfos = Physics.OverlapBox(player.transform.position + Vector3.up + _attackDir, _resourceConfig.attackBoxSize / 2, _attackRot);
            
            foreach (var h in hitInfos)
            {
                if (!h.transform.root.CompareTag("Entity"))
                    continue;
                if (h.transform.root.GetComponent<Entity>().CanOperate(OperateKey.L, playerInventory.GetItem()))
                {
                    h.transform.root.GetComponent<Entity>().Operate(OperateKey.L, player, playerInventory.GetItem());
                    playerInventory.ReduceDurability();
                }
                
            }
            
            DisplayBox.Display(player.transform.position + Vector3.up + _attackDir,_resourceConfig.attackBoxSize / 2, _attackRot, Color.red, 0.1f);
        }

        private void StateInit()
        {
            _hitTransform = playerModal.PlayerHitInfo.Value;
            if (_hitTransform != null)
            {
                _attackDir = new Vector3((_hitTransform.position - player.transform.position).x, 0, (_hitTransform.position - player.transform.position).z).normalized;
                _attackRot = Quaternion.LookRotation(_attackDir);
            }
            else
            {
                _attackDir = new Vector3((cameraModal.MouseTarget.Value - player.transform.position).x, 0, (cameraModal.MouseTarget.Value - player.transform.position).z).normalized;
                _attackRot = Quaternion.LookRotation(_attackDir);
            }

            _hitTransform = null;
            animator.Play(GetAnimName(), _resourceConfig.animLayer, 0);
        }
        
        public override UniTask GetAnimPlayTask()
        {
            AnimPlayTask = UniTask.WaitUntil(() => GetAnimPlayProgress() > 0.99f && IsCurrentAnimPlaying(GetAnimName()));
            return AnimPlayTask;
        }
    }
}